////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   
// AUTHOR           :    
// DATE CREATE      :   
// DESCRIPTION      :   
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  ----------------------------
// 2006/04/26   Ric Yik         1.0                 BugZilla #5535
//
//
////////////////////////////////////////////////////////////////////////////////
package com.erp.framework.model.schema;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.erp.framework.SimpleConstants;
import com.erp.framework.util.BeanUtil;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.apaches.commons.collection.map.LinkedMap;
import com.erp.framework.util.logger.Logger;

public class TableSchema extends ArrayList implements SimpleConstants {

    protected final Set userFieldNames = new HashSet(Arrays.asList(new String[] { "createUser", "updateUser", "hdrOwner" }));

    protected Map ranges = new LinkedMap();

    protected class TableSchemaArrayList extends ArrayList {

        public boolean add(Object o) {
            if (o instanceof MetaData) {
                MetaData md = (MetaData) o;
                try {
                    md.setTableName(CommonUtil.getString(BeanUtil.getConstant(TableSchema.this.getClass(), "TABLE_NAME")));
                    if (userFieldNames.contains(md.getVoPropertyName())) {
                        md.setStereoType(StereoType.USER);
                    }
                    //if (md.isRangeFrom()) ranges.put(md.getVoPropertyName(), md);
                } catch (Exception e) {
                    Logger.debug(o, e.getMessage());
                }
            }
            return super.add(o);
        }

    }

	public static final String SCHEMA_LEFT = "LEFT";
    public static final String SCHEMA_RIGHT = "RIGHT";
    public static final String SCHEMA_BOTH = "BOTH";
    
    protected ArrayList ts = new TableSchemaArrayList(); // new way

    protected Map voNameIndex = new HashMap();
    protected Map objectIdIndex = new HashMap();

    public ArrayList getTs() {
        return ts;
    }

    public static void setTs(ArrayList ts) {
    	ts = ts;
    }

    public MetaData getMetaDataByVoName(String voName){
        if (this.voNameIndex.containsKey(voName)) return (MetaData) this.voNameIndex.get(voName);
        MetaData md = null;
        for(int i = 0; (ts != null) && (i < ts.size()); i++){
            md = (MetaData)ts.get(i);
            if(voName.equals(md.getVoPropertyName())){
                this.voNameIndex.put(md.getVoPropertyName(), md);
                if (!CommonUtil.isEmpty(md.getWebObjectId()) && !this.objectIdIndex.containsKey(md.getWebObjectId())) this.objectIdIndex.put(md.getWebObjectId(), md);
                return md;
            }
        }
        return null;
    }
    
    public MetaData getMetaDataByColumnName(String columnName){
        MetaData md = null;
        for(int i = 0; (ts != null) && (i < ts.size()); i++){
            md = (MetaData)ts.get(i);
            if(columnName.equals(md.getColumnName())){
                return md;
            }
        }
        return null;
    }

    public MetaData getMetaDataByObjectId(String objectId){
        if (this.objectIdIndex.containsKey(objectId)) return (MetaData) this.objectIdIndex.get(objectId);
        MetaData md = null;
        for(int i = 0; (ts != null) && (i < ts.size()); i++){
            md = (MetaData)ts.get(i);
            if(objectId.equals(md.getWebObjectId())){
                this.objectIdIndex.put(md.getWebObjectId(), md);
                if (!CommonUtil.isEmpty(md.getVoPropertyName()) && !this.voNameIndex.containsKey(md.getVoPropertyName())) this.voNameIndex.put(md.getVoPropertyName(), md);
                return md;
            }
        }
        return null;
    }
    
    public int getColSizeByVoName(String voName){
        MetaData md = this.getMetaDataByVoName(voName);
        return (md == null) ? 0 : md.getColumnSize();
    }
    
    public int getColSizeByColumnName(String columnName){
        MetaData md = null;
        for(int i = 0; (ts != null) && (i < ts.size()); i++){
            md = (MetaData)ts.get(i);
            if(columnName.equals(md.getColumnName())){
                return md.getColumnSize();
            }
        }
        return 0;
    }
    
    public int getColSizeByObjectId(String webObjectId){
        MetaData md = this.getMetaDataByObjectId(webObjectId);
        return (md == null) ? 0 : md.getColumnSize();
    }
    
    public int getDisplaySizeByVoName(String voName){
        MetaData md = this.getMetaDataByVoName(voName);
        return (md == null) ? 0 : md.getDisplaySize();
    }
    
    public int getDisplaySizeByColumnName(String columnName){
        MetaData md = null;
        for(int i = 0; (ts != null) && (i < ts.size()); i++){
            md = (MetaData)ts.get(i);
            if(columnName.equals(md.getColumnName())){
                return md.getDisplaySize();
            }
        }
        return 0;
    }
    
    public int getDisplaySizeByObjectId(String webObjectId){
        MetaData md = this.getMetaDataByObjectId(webObjectId);
        return (md == null) ? 0 : md.getDisplaySize();
    }
    
    public String getDisplayWebLabelByVoName(String voName){
        MetaData md = this.getMetaDataByVoName(voName);
        return (md == null) ? voName : md.getWebLabelName();
    }
    
    public String getDisplayWebLabelByColumnName(String columnName){
        MetaData md = null;
        for(int i = 0; (ts != null) && (i < ts.size()); i++){
            md = (MetaData)ts.get(i);
            if(columnName.equals(md.getColumnName())){
                return md.getWebLabelName();
            }
        }
        return columnName;
    }

    public String getDisplayWebLabelByObjectId(String webObjectId){
        MetaData md = this.getMetaDataByObjectId(webObjectId);
        return (md == null) ? webObjectId : md.getWebLabelName();
    }
    
    public String getDisplayWebLabelByObjectIdWithChar(String webObjectId, String character, String alignment){
    	String webLabel = getDisplayWebLabelByObjectId(webObjectId);
    	StringBuffer sb = new StringBuffer();
    	
    	if(SCHEMA_LEFT.equals(alignment)){
    		sb.append(character).append(webLabel);
    	} else if(SCHEMA_RIGHT.equals(alignment)){
    		sb.append(webLabel).append(character);
    	} else if(SCHEMA_BOTH.equals(alignment)){
    		sb.append(character).append(webLabel).append(character);
    	}
    	
    	return sb.toString();
    }
    
    public String getDisplayWebLabelByObjectIdWithChars(String webObjectId, String leftChar, String rightChar){
    	String webLabel = getDisplayWebLabelByObjectId(webObjectId);
    	StringBuffer sb = new StringBuffer();
    	
    	sb.append(leftChar).append(webLabel).append(rightChar);    	
    	
    	return sb.toString();
    }
    
    public String getTabLabelByObjectId(String webObjectId){
    	return getDisplayWebLabelByObjectIdWithChars(webObjectId, "[", "] ");     	
    }
    
    public String getDisplayWebLabelWithColonByObjectId(String webObjectId){
    	return getDisplayWebLabelByObjectIdWithChar(webObjectId, ":", SCHEMA_RIGHT);     	
    }
    
    public int getTextAreaColSizeByVoName(String voName){
        MetaData md = this.getMetaDataByVoName(voName);
        return (md == null) ? 0 : md.getTextAreaColSize();
    }
    
    public int getTextAreaColSizeByColumnName(String columnName){
        MetaData md = null;
        for(int i = 0; (ts != null) && (i < ts.size()); i++){
            md = (MetaData)ts.get(i);
            if(columnName.equals(md.getColumnName())){
                return md.getTextAreaColSize();
            }
        }
        return 0;
    }
    
    public int getTextAreaColSizeByObjectId(String webObjectId){
        MetaData md = this.getMetaDataByObjectId(webObjectId);
        return (md == null) ? 0 : md.getTextAreaColSize();
    }

    public int getTextAreaRowSizeByVoName(String voName){
        MetaData md = this.getMetaDataByVoName(voName);
        return (md == null) ? 0 : md.getTextAreaRowSize();
    }
    
    public int getTextAreaRowSizeByColumnName(String columnName){
        MetaData md = null;
        for(int i = 0; (ts != null) && (i < ts.size()); i++){
            md = (MetaData)ts.get(i);
            if(columnName.equals(md.getColumnName())){
                return md.getTextAreaRowSize();
            }
        }
        return 0;
    }
    
    public int getTextAreaRowSizeByObjectId(String webObjectId){
        MetaData md = this.getMetaDataByObjectId(webObjectId);
        return (md == null) ? 0 : md.getTextAreaRowSize();
    }

    public Map getRanges() {
        return ranges;
    }

    public void setRanges(Map ranges) {
        this.ranges = ranges;
    }

}
